class HotKeys {
  constructor (player) {
    this.player = player

    this.subEvent()
  }

  subEvent () {
    const player = this.player
    document.addEventListener('keydown', e => {
      if (player._status.focus) {
        const tag = document.activeElement.tagName.toUpperCase()
        const editable = document.activeElement.getAttribute('contenteditable')
        if (
          tag !== 'INPUT' &&
          tag !== 'TEXTAREA' &&
          editable !== '' &&
          editable !== 'true'
        ) {
          const event = e || window.event
          let percentage
          switch (event.keyCode) {
            case 32:
              event.preventDefault()
              player.toggle()
              break
            case 37:
              event.preventDefault()
              player.seek(player.dom.video.currentTime - 5)
              player.controller.controls.setAutoHide()
              break
            case 39:
              event.preventDefault()
              player.seek(player.dom.video.currentTime + 5)
              player.controller.controls.setAutoHide()
              break
            case 38:
              event.preventDefault()
              percentage = player.getVolume() + 0.1
              player.setVolume(percentage)
              break
            case 40:
              event.preventDefault()
              percentage = player.getVolume() - 0.1
              player.setVolume(percentage)
              break
          }
        }
      }
    })
  }
}

export default HotKeys
